/* eslint-disable no-lonely-if */
/* eslint-disable import/prefer-default-export */
/* eslint-disable no-param-reassign */
import { PerspectiveCamera, WebGLRenderer } from 'three'

/**
 * 监听 resize 事件
 */
export const listenResize = (
  sizes: {
    width: number
    height: number
  },
  camera: PerspectiveCamera,
  renderer: WebGLRenderer,
) => {
  window.addEventListener('resize', () => {
    console.log("resize")
    // update sizes
    sizes.width = window.innerWidth
    sizes.height = window.innerHeight

    // update camera
    camera.aspect = sizes.width / sizes.height
    camera.updateProjectionMatrix()

    // update renderer
    renderer.setSize(sizes.width, sizes.height)
    renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
  })
}

/**
 * 全屏
 */
export const setFullScreen = (canvas: HTMLElement) => {
  // @ts-ignore
  const fullscreenElement = document.fullscreenElement || document.webkitFullscreenElement
  if (fullscreenElement) {
    if (document.exitFullscreen) {
      document.exitFullscreen()
    } else {
      // @ts-ignore
      document.webkitExitFullscreen()
    }
  } else {
    if (canvas.requestFullscreen) {
      canvas.requestFullscreen()
    } else {
      // @ts-ignore
      canvas.webkitRequestFullscreen()
    }
  }
}

/**
 * 双击全屏
 */
export const dbClkfullScreen = (canvas: HTMLElement) => {
  window.addEventListener('dblclick', () => {
    setFullScreen(canvas)
  })
}

export const randomSection = (a: number, b: number) => {
  let maxNum = Math.max(a, b);
  let minNum = Math.min(a, b);
  return Math.random() * (maxNum - minNum) + minNum
}

/**
 * 改变颜色
 * @param col 颜色代码，＃开头
 * @param amt 正数变深，负数变浅
 * @returns 
 */
export function changeColor(col: string, amt: number) {
  var usePound = false;
  if (col[0] == "#") {
    col = col.slice(1);
    usePound = true;
  }
  var num = parseInt(col, 16);
  var r = (num >> 16) + amt;
  if (r > 255) r = 255;
  else if (r < 0) r = 0;
  var b = ((num >> 8) & 0x00FF) + amt;
  if (b > 255) b = 255;
  else if (b < 0) b = 0;
  var g = (num & 0x0000FF) + amt;
  if (g > 255) g = 255;
  else if (g < 0) g = 0;
  return (usePound ? "#" : "") + (g | (b << 8) | (r << 16)).toString(16);
}